<template>
  <el-dialog width="50%" :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
        <el-col :span="24">
          <!-- 通道类型 0-内置通道 1-自定义灵活通道 -->
          <el-form-item label="通道类型" prop="channelType">
            <!--文本框-->
            <el-radio-group v-model="dataForm.channelType">
              <el-radio :label="'0'">内置通道</el-radio>
              <el-radio :label="'1'">自定义通道</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col v-if="dataForm.channelType==='0'" :span="13">
          <!-- 通道编码 -->
          <el-form-item label="通道编码" prop="channelCode">
            <!--文本框-->
            <el-select v-model="dataForm.channelCode" placeholder="通道编码" @change="getTemplateList" clearable>
              <el-option v-for="template in $getDictList('MSG_CHANNEL', true)" :key="template.val" :label="template.label"
                :value="template.val"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="dataForm.channelType==='0'" :span="13">
          <!-- 模板编码 -->
          <el-form-item label="模板编码" prop="templateId">
            <!--文本框-->
            <el-select v-model="dataForm.templateId" placeholder="模板编码" clearable>
              <el-option v-for="template in this.templateList" :key="template.templateId" :label="template.templateName"
                :value="template.templateId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="dataForm.channelType==='1'" :span="13">
          <!-- 自定义类名 -->
          <el-form-item label="自定义类名" prop="customClass">
            <!--文本框-->
            <el-input v-model="dataForm.customClass" placeholder="自定义类名" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col v-if="dataForm.channelType==='1'" :span="13">
          <!-- 自定义方法名 -->
          <el-form-item label="自定义方法名" prop="customMethod">
            <!--文本框-->
            <el-input v-model="dataForm.customMethod" placeholder="自定义方法名" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
import { Message } from 'element-ui'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      templateList: [],
      dataForm: {
        id: '',
        bizTypeCode: '',
        channelType: '0',
        channelCode: '',
        templateId: '',
        customClass: '',
        customMethod: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        bizTypeCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        channelType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          this.getInfo()
        }
      })
    },
    // 获取模板列表
    getTemplateList (val) {
      let templateUrl = ''
      this.templateList = []
      if (val === 'SMS') {
        templateUrl = '/msg/msgSmsTemplate/list'
      } else if (val === 'EMAIL') {
        templateUrl = '/msg/msgMailTemplate/list'
      } else if (val === 'WECHAT') {
        templateUrl = '/msg/msgWechatTemplate/list'
      } else if (val === 'DINGTALK') {
        templateUrl = '/msg/msgDingtalkTemplate/list'
      } else if (val === 'SITE') {
        templateUrl = '/msg/msgRecordTemplate/list'
      }
      this.$http.get(`${templateUrl}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.templateList = res.data
      }).catch(() => { })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/msg/msgBizChannel/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        this.getTemplateList(res.data.channelCode)
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        if (this.dataForm.channelType === '0') {
          this.dataForm.customClass = ''
          this.dataForm.customMethod = ''
          if (!this.dataForm.channelCode) {
            return Message.error('通道编码不能为空')
          }
          if (!this.dataForm.templateId) {
            return Message.error('模板编码不能为空')
          }
        } else {
          this.dataForm.channelCode = ''
          this.dataForm.templateId = ''
          if (!this.dataForm.customClass) {
            return Message.error('自定义类不能为空')
          }
          if (!this.dataForm.customMethod) {
            return Message.error('自定义方法不能为空')
          }
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/msg/msgBizChannel/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
